package com.xysoft.framework.mvc;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.mvc.multiaction.AbstractUrlMethodNameResolver;
import org.springframework.web.util.WebUtils;

public class PrefixMethodNameResolver extends AbstractUrlMethodNameResolver {
	
	private String prefix = "";
	
	private final Map<String, String> methodNameCache = new ConcurrentHashMap<String, String>();
	
	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}
	
	public PrefixMethodNameResolver() {
		
	}
	
	public PrefixMethodNameResolver(String prefix) {
		this.prefix = prefix;
	}

	@Override
	protected String getHandlerMethodNameForUrlPath(String urlPath) {
		String methodName = this.methodNameCache.get(urlPath);
		if (methodName == null) {
			methodName = String.format("%s%s", prefix, StringUtils.capitalize(WebUtils.extractFilenameFromUrlPath(urlPath)));
			this.methodNameCache.put(urlPath, methodName);
		}
		return methodName;
	}

}
